#!/usr/bin/python

##################################
#                                #
#  David Lladro - S2 Grupo       #
#     Noviembre 2011             #
#  Mail: david.lladro@gmail.com  #
#  Twitter: @davidlladro         #
#                                #
##################################

# paquetes necesarios 
import urllib2
import BeautifulSoup
import subprocess
import time
# Host y archivo 
url_base = "http://search.4shared.com/q/0"
url_final= "0/*.apk"

for i in range(0,90,1):
	base = url_base + str(i) + url_final
	html = urllib2.urlopen(str(base)).read()
	#print html
	html = html.replace("\\\'","\'")
	soup = BeautifulSoup.BeautifulSoup(html)
	table = soup.find("table", "listView")
	#print table
	for div in table.findAll("div", 'imgbox'):
		href = div.a['href']
		#print div
		print href
		html2 = urllib2.urlopen(str(href)).read()
		apk = str(html2).split("function startDownload")[1]
		apk = apk.split("\"")[1]
		print apk
		nombre = apk.split("?")[0]
		nombre = nombre.split("/")[5]
		time.sleep(15)
		conversion = subprocess.Popen(['wget', apk, '-O', nombre], stderr=subprocess.STDOUT,stdout = subprocess.PIPE )
		out, err = conversion.communicate()
		print out
